﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Extract Palindromes</title>
	<link href="styles/js-console.css" rel="stylesheet" />
	<script src="scripts/string-extensions.js"></script>
</head>
<body>							  

	<label for="text">Write a text with some palindromes in it(e.g. lamal,abba) </label>
	<input id="text" type="text" class="js-console-input" />
    <br />
	<a href="#" class="js-console-btn" onclick="findPolindromes()">Find the palindromes!</a>
    <br />
    <br />
	<div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
	<script>

	    function findPolindromes() {
	        var strBuilder = buildStringBuilder();
	        var text = jsConsole.read("#text");
	        text = text.toLowerCase();
	        text = text.replace(".", " ").replace("!", " ").replace("?", " ").replace(",", " ");
	        var wordsArr = text.split(" ");
	        for (var i = 0; i < wordsArr.length; i++) {
	            strBuilder = buildStringBuilder();
	            for (var j = wordsArr[i].length - 1; j >= 0 ; j--) {
	                strBuilder.append(wordsArr[i][j]);
	            }
	            if (strBuilder.toString() == wordsArr[i]) {
	                jsConsole.writeLine(wordsArr[i]);
	            }
	        }
	        //jsConsole.writeLine(text);
	    }
	    function buildStringBuilder() {
	        return {
	            strs: [],
	            len: 0,
	            append: function (str) {
	                this.strs[this.len++] = str;
	                return this;
	            },
	            toString: function () {
	                return this.strs.join("");
	            }
	        };
	    }


	</script>

</body>
</html>